
package com.contractmodule.servlet;
import com.companymodule.dao.TbCompanyDAO;
import com.companymodule.dao.TbCompanyProductTypeMiddleDAO;
import com.contractmodule.bean.TbDispatchList;
import com.contractmodule.bean.TbDispatchListProductPrice;
import com.contractmodule.dao.*;
import com.usersmodule.bean.TbUsers;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbDispatchListServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        int userId=((TbUsers) request.getSession().getAttribute("Users")).getUserId();
        if("add".equals(method)){
        String dispatchListCode=request.getParameter("dispatchListCode");
        String contractCode=request.getParameter("contractCode");
        int productTypeId=Integer.parseInt(request.getParameter("productTypeId"));
        String dispatchListName=request.getParameter("dispatchListName");
        String dispatchListBudget=request.getParameter("dispatchListBudget");
        int companyId = Integer.parseInt(request.getParameter("companyId"));
        int productId = Integer.parseInt(request.getParameter("productId"));
        int companyProductTypeMiddleId = new TbCompanyProductTypeMiddleDAO().getTbCompanyProductTypeMiddleByProductTypeId(productTypeId, companyId).get(0).getCompanyProductTypeMiddleId();
            if(new TbDispatchListDAO().add(new TbDispatchList(dispatchListCode,contractCode,companyProductTypeMiddleId,dispatchListName,dispatchListBudget,userId))){
            	if(new TbDispatchListProductPriceDAO().add(new TbDispatchListProductPrice(dispatchListCode,productId))){
            		request.getSession().setAttribute("result", "添加成功");
            	}
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
            goList(request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_DispatchList", "DispatchListId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);
        }
        if("edit".equals(method)){
        int dispatchListId=Integer.parseInt(request.getParameter("dispatchListId"));
        String dispatchListCode=request.getParameter("dispatchListCode");
        String contractCode=request.getParameter("contractCode");
        int companyProductTypeMiddleId=Integer.parseInt(request.getParameter("companyProductTypeMiddleId"));
        String dispatchListName=request.getParameter("dispatchListName");
        String dispatchListBudget=request.getParameter("dispatchListBudget");
            if(new TbDispatchListDAO().edit(new TbDispatchList(dispatchListId,dispatchListCode,contractCode,companyProductTypeMiddleId,dispatchListName,dispatchListBudget,userId))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        if ("list".equals(method))goList(request, response);
        if ("listById".equals(method)) {
        	request.getSession(true).setAttribute("ConstractListALL", new TbContractDAO().getTbContractAll());
            request.getSession(true).setAttribute("tbDispatchList",new TbDispatchListDAO().getTbDispatchListByDispatchListId(Integer.parseInt(request.getParameter("dispatchListId"))));
            response.sendRedirect("ContractModule/TbDispatchListOperate.jsp");
    	}
        if("showadd".equals(method)){
        	//request.getSession(true).setAttribute("ConstractListALL", new TbContractDAO().getTbContractAll());
            request.getSession(true).removeAttribute("tbDispatchList");
            request.getSession().setAttribute("companyList", new TbCompanyDAO().getTbCompanyAll());//get all companys
            response.sendRedirect("ContractModule/TbDispatchListOperate.jsp");
        }
        if("listbycode".equals(method)){
        	request.getSession(true).removeAttribute("tbDispatchListList");
        	String contractCode=request.getParameter("contractCode");
        	System.out.println("contractCode=="+contractCode);
        	request.getSession(true).setAttribute("tbDispatchListList",new TbDispatchListDAO().getTbDispatchListByContractCode(contractCode) );
        	response.sendRedirect("ContractModule/TbDispatchListList.jsp");
        }
        if("dispatchInfo".equals(method)){//get the info of Dispatch
        	request.getSession().setAttribute("tbDispatchList", new TbDispatchListDAO().getTbDispatchListByDispatchListId(Integer.parseInt(request.getParameter("dispatchListId"))));
        	response.sendRedirect("ContractModule/TbDispatchInfo.jsp");
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession().setAttribute("tbContractList",new TbContractDAO().getTbContractByIsArchive());
    	request.getSession(true).setAttribute("tbDispatchListList",new TbDispatchListDAO().getTbDispatchListAll());
        response.sendRedirect("ContractModule/TbDispatchListList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
}
